package es.dk.hl.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.facebook.commands.batch.BatchRun;
	import com.facebook.commands.data.CreateObject;
	import com.facebook.commands.data.GetAssociatedObjects;
	import com.facebook.commands.data.SetAssociation;
	import com.facebook.commands.users.GetInfo;
	import com.facebook.data.NumberResultData;
	import com.facebook.data.StringResultData;
	import com.facebook.data.auth.GetSessionData;
	import com.facebook.data.batch.BatchCollection;
	import com.facebook.data.batch.BatchResult;
	import com.facebook.data.users.FacebookUser;
	import com.facebook.data.users.GetInfoData;
	import com.facebook.data.users.GetInfoFieldValues;
	import com.facebook.events.FacebookEvent;
	import com.facebook.utils.FacebookSessionUtil;
	
	import es.dk.hl.event.FBEvent;
	import es.dk.hl.model.HomeLinkModelLocator;

	public class FaceBook_PreLogin_Command implements ICommand
	{

		protected static var API_KEY:String = 'f1962ffec7e7b67235d7c86f6a18fc5a';
		protected static var SECRET_KEY:String = '930855fa58c860f7d75c88b89dd5c254';

		public function FaceBook_PreLogin_Command()
		{
		}
		
		public function execute(event:CairngormEvent):void {
			HomeLinkModelLocator.getInstance().facebookSessionUtil = new FacebookSessionUtil(API_KEY,SECRET_KEY,HomeLinkModelLocator.getInstance().loaderInfoToFacebook);
			HomeLinkModelLocator.getInstance().facebookSessionUtil.addEventListener(FacebookEvent.CONNECT,onFacebookEventConnect);
			HomeLinkModelLocator.getInstance().facebookSessionUtil.addEventListener(FacebookEvent.LOGIN_SUCCESS,onFacebookEventLoginSuccess);
			HomeLinkModelLocator.getInstance().facebookSessionUtil.addEventListener(FacebookEvent.LOGIN_FAILURE,onFacebookEventLoginFailure);
			HomeLinkModelLocator.getInstance().facebookSessionUtil.addEventListener(FacebookEvent.LOGIN_WINDOW_SHOW,onFacebookEventLoginWindowShow);
			HomeLinkModelLocator.getInstance().facebookSessionUtil.addEventListener(FacebookEvent.LOGOUT,onFacebookEventLogout);
			HomeLinkModelLocator.getInstance().facebookSessionUtil.addEventListener(FacebookEvent.WAITING_FOR_LOGIN,onFacebookEventWaitingForLogin);
			HomeLinkModelLocator.getInstance().facebook = HomeLinkModelLocator.getInstance().facebookSessionUtil.facebook;
			new FBEvent(FBEvent.FBEvent_VALIDATELOGIN_LOGIN).dispatch();
			HomeLinkModelLocator.getInstance().tip = "Login to Facebook. After login, please push Validate Button";
		}
		
		protected function onFacebookEventConnect(event:FacebookEvent):void {
			if (event.success) {
				HomeLinkModelLocator.getInstance().statusFaceBookConnect = HomeLinkModelLocator.statusFaceBookConnect_CONNECTED;
				var batch:BatchCollection = new BatchCollection();
				var fbUserId:String = (event.data as GetSessionData).uid; 				
				batch.addItem(new GetInfo([fbUserId], [GetInfoFieldValues.FIRST_NAME, GetInfoFieldValues.CURRENT_LOCATION]));
				batch.addItem(new GetAssociatedObjects("usercompo",fbUserId));
				var batchRun:BatchRun = HomeLinkModelLocator.getInstance().facebook.post(new BatchRun(batch)) as BatchRun;
				batchRun.addEventListener(FacebookEvent.COMPLETE, onBatchComplete);
			} else {
				HomeLinkModelLocator.getInstance().facebookSessionUtil.login(false);
				HomeLinkModelLocator.getInstance().statusFaceBookConnect = HomeLinkModelLocator.statusFaceBookConnect_CONNECT;
			}
			
		}
		
		protected function onBatchComplete(event:FacebookEvent):void {
			if (event.success) {
				var results:Array = (event.data as BatchResult).results;
				var getInfoResult:GetInfoData = results[0];					
				HomeLinkModelLocator.getInstance().facebookUser = getInfoResult.userCollection.getItemAt(0) as FacebookUser;
				HomeLinkModelLocator.getInstance().nameUser = HomeLinkModelLocator.getInstance().facebookUser.first_name;
				var xml:XML = new XML(StringResultData(results[1]).rawResult.toString());
				
				var data_setAssociation:XML = XML(xml.children()[0]);
				var object_association:XML = XML(data_setAssociation.children()[0]);
				var id2:XML = XML(object_association.children()[0]);

				var s_oidcomposicion:String = String(id2);

				
				if (s_oidcomposicion != null){
					HomeLinkModelLocator.getInstance().composicion_id = Number(s_oidcomposicion);
				}else {
					new FBEvent(FBEvent.FBEvent_CREATEANDSET).dispatch();					
				}
			}
		}
		
		protected function onFacebookEventLoginSuccess(event:FacebookEvent):void {
			HomeLinkModelLocator.getInstance().tip = "Login Success in Facebook";
		}
		
		protected function onFacebookEventLoginFailure(event:FacebookEvent):void {
			HomeLinkModelLocator.getInstance().tip = "Login Failure in Facebook";
		}
		
		protected function onFacebookEventLoginWindowShow(event:FacebookEvent):void {
			HomeLinkModelLocator.getInstance().tip = "Login Window  Show in Facebook";
		}
		
		protected function onFacebookEventLogout(event:FacebookEvent):void {
			HomeLinkModelLocator.getInstance().tip = "Logout in Facebook";
		}
		
		protected function onFacebookEventWaitingForLogin(event:FacebookEvent):void {
			HomeLinkModelLocator.getInstance().tip = "Waiting for Login in Facebook";
		}								
	}
}